package mint.modules;


import java.io.IOException;
import java.io.InputStream;
import java.util.Formatter;

import mint.MintLexer;
import mint.tools.CLI.CLI;

import org.antlr.runtime.ANTLRInputStream;
import org.antlr.runtime.RecognitionException;
import org.antlr.runtime.Token;

public class LexicalAnalizer {
	public static void lexer(CLI cli, InputStream inputStream, Formatter output) throws IOException {
		MintLexer lexer = new MintLexer(new ANTLRInputStream(inputStream));
		Token token;
		boolean done = false;
		
		while (!done) {
			try {
				for (token = lexer.nextToken(); token.getType() != MintLexer.EOF; token = lexer.nextToken()) {
					if (token.getChannel() != MintLexer.HIDDEN) {				
						String type = "";
						String text = token.getText();
						int line = token.getLine();
						
						switch (token.getType()) {
						case MintLexer.Identifier:
							type = " IDENTIFIER " + text;
							break;
						case MintLexer.CharLiteral:
							type = " CHAR " + text;
							break;					
						case MintLexer.FloatingPointLiteral:
							type = " FLOAT " + text;
							break;					
						case MintLexer.IntegerLiteral:
							type = " INTEGER " + text;
							break;					
						case MintLexer.StringLiteral:
							type = " STRING " + text;
							break;
						case MintLexer.Indent:
							type = " INDENT";
							break;
						case MintLexer.Dedent:
							type = " DEDENT";
							break;
						case MintLexer.EOF:
							break;
						default:
							type = " " + text;
						}
						
						if (!type.isEmpty())
							output.format("%s\n", line + type);
					}
				}
				done = true;
			} catch (Exception e) {
				// print the error:
				System.out.println(cli.getInFile() + " " + e);
				lexer.reportError((RecognitionException) e);
			}
		}
	}
}
